package com.levy.contronller.lamdaTest.stream;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamTest2 {

    public static void main(String[] args) {

        Stream<String> stream = Stream.of("a", "b", "c");
//        String[] strings = stream.toArray(o -> new String[o]);
        String[] strings = stream.toArray(String[]::new);
        Arrays.asList(strings).forEach(System.out::println);

        List<String> collect = stream.collect(Collectors.toList());

        List<String> collect1 = stream.collect(() -> new LinkedList<>(), (list1, item) -> list1.add(item),
                (list1, list2) -> list1.addAll(list2));

        List<String> collect2 = stream.collect(LinkedList::new, LinkedList::add, LinkedList::addAll);


    }

    @Test
    public void intStreamTest() {

        IntStream.of(new int[]{3, 4, 5}).forEach(System.out::println);

        IntStream.range(3, 8).forEach(System.out::println);

        IntStream.rangeClosed(3, 8).forEach(System.out::println);

    }


    @Test
    public void test() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
//        Integer reduce = list.stream().map(i -> 2 * i).reduce(1, (a, b) -> Integer.sum(a, b));
        Integer reduce = list.stream().map(i -> 2 * i).reduce(0, Integer::sum);
        System.out.println(reduce);

    }


    @Test
    public void test2() {
        Stream<String> stream = Stream.of("a", "b", "c");
        String s = stream.collect(Collectors.joining()).toString();
        System.out.println(s);

        ArrayList<String> collect = stream.collect(Collectors.toCollection(ArrayList::new));
        collect.forEach(System.out::println);


        TreeSet<String> collect1 = stream.collect(Collectors.toCollection(TreeSet::new));
        collect1.forEach(System.out::println);


    }

    @Test
    public void test3() {
        Stream<String> generate = Stream.generate(UUID.randomUUID()::toString);
        generate.findFirst().ifPresent(System.out::println);

        Stream.iterate(1, item -> item + 2).limit(6).forEach(System.out::println);

    }

    @Test
    public void test4() {
        Stream<Integer> limit = Stream.iterate(1, item -> item + 2).limit(6);
//        IntSummaryStatistics intSummaryStatistics = limit.filter(o -> o > 2).mapToInt(o -> o * 2).summaryStatistics();
//        System.out.println(intSummaryStatistics.getMax());
//        System.out.println(intSummaryStatistics.getAverage());
//        System.out.println(intSummaryStatistics.getSum());
//        System.out.println(intSummaryStatistics.getCount());

        System.out.println(
                limit.filter(o -> o > 2)
                        .mapToInt(o -> o * 2)
                        .skip(2)
                        .limit(2)
                        .sum()
        );

    }


    @Test
    public void test5() {
        List<String> list = Arrays.asList("agf", "bhg", "cfgh");
        list.stream().map(item -> item.substring(0, 1).toUpperCase() + item.substring(1))
                .forEach(System.out::println);

        //没有终止操作符的情况下 这些流中的运算不会执行
        list.stream().map(item -> {
            System.out.println("123");
            return item;
        });
    }

    @Test
    public void test6() {
//        IntStream.iterate(0,i -> (i+1)%2).distinct().limit(6).forEach(System.out::println);
        //distinct 一般放在所有延时运算的最后 否则计算会停不下来
        IntStream.iterate(0, i -> (i + 1) % 2).limit(6).distinct().forEach(System.out::println);
    }


}
